import './style.scss';
import { useState } from 'react';
import classNames from 'classnames';
import DeleteButton from './delete';
const ButtonToolList = ({
    data,
    imageUrl,
    index,
    classname = '',
    loadVideo,
    inDetailPop = false, // 是否在弹窗中使用
    onClosePop, // 关闭弹窗的函数
}) => {
    const [islike, setIsLike] = useState(data.likelist?.indexOf(index) > -1);
    // 处理删除后的额外逻辑
    const handleDeleteComplete = () => {
        // 如果在弹窗中且提供了关闭函数，则关闭弹窗
        if (inDetailPop && onClosePop) {
            onClosePop();
        }
        loadVideo();
    };
    const handleDownload = async (event) => {
        event.stopPropagation();
        if (!imageUrl) return;

        try {
            const link = document.createElement('a');
            link.href = imageUrl; // 直接使用远程 URL
            const fileName =
                data.type === 'image'
                    ? imageUrl.split('/').pop() || 'image.jpg'
                    : imageUrl.split('/').pop() || `video_${Date.now()}.mp4`;
            link.download = fileName; // 设置下载文件名
            link.target = '_blank';
            link.click();
            // 无需资源清理
        } catch (error) {
            console.error('下载失败:', error);
        }
    };

    return (
        <>
            <div
                className={classNames(
                    `button-download-lick-delete-list ${classname}`,
                    { islike: islike }
                )}
            >
                <div className="tool-btn download-btn" onClick={handleDownload}>
                    <img
                        src="https://img.alicdn.com/imgextra/i2/O1CN01mJxBr31Lxg76r5fVN_!!6000000001366-55-tps-24-24.svg"
                        alt=""
                    />
                </div>

                <DeleteButton
                    data={{ jobId: data.jobId, no: index }}
                    handleDeleteComplete={handleDeleteComplete}
                />
                {/* <div
                    className="tool-btn like-btn"
                    onClick={() => {
                        console.log(index, 'detelt');
                    }}
                >
                    <img
                        src={
                            islike
                                ? 'https://img.alicdn.com/imgextra/i1/O1CN013dDzTq1s1P2VfXYOJ_!!6000000005706-55-tps-24-24.svg'
                                : 'https://img.alicdn.com/imgextra/i2/O1CN01SA24R71WIQD4xc0Wy_!!6000000002765-55-tps-24-24.svg'
                        }
                        alt=""
                    />
                </div> */}
            </div>
        </>
    );
};

export default ButtonToolList;
